package com.starsoft.smdc.service;

import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.dao.FundamentalDao;
import com.starsoft.smdc.dao.MarketDailyDao;
import com.starsoft.smdc.dao.SmdcSecurityDao;
import com.starsoft.smdc.util.SecurityType;

@Service
public class SecurityService {

	@Autowired
	private SmdcSecurityDao securityDao;
	
	@Autowired
	private MarketDailyDao marketDao;
	
	@Autowired
	private FundamentalDao fundamentalDao;

	public SmdcSecurity get(String id) {
		return securityDao.get(id);
	}

	public List<SmdcSecurity> get(List<String> secIds) {
		return securityDao.get(secIds);
	}

	public SmdcSecurity getFirstByTicker(String ticker) {
		List<SmdcSecurity> secs = securityDao.getByTicker(ticker);
		if (secs.isEmpty()) {
			return null;
		}
		return secs.get(0);
	}

	public List<SmdcSecurity> getByTicker(String ticker) {
		return securityDao.getByTicker(ticker);
	}

	public void saveOrUpdate(SmdcSecurity stock) {
		securityDao.saveOrUpdate(stock);
	}

	public List<SmdcSecurity> find(String hql, Object... args) {
		return securityDao.find(hql, args);
	}

	public List<SmdcSecurity> getAll() {
		return securityDao.getAll();
	}

	public Date getBeginDate(SmdcSecurity security) {
		return securityDao.getBeginDate(security);
	}

	public Map<String, Object> pageQuery(PageObject po) {
		return securityDao.pageQuery(po);
	}

	public Map<String, Object> pageQuery(PageObject po, String secId, String exchange, String secType, String fundCategory) {
		return securityDao.pageQuery(po, secId, exchange, secType, fundCategory);
	}
	
	public Map<String, Object> pageQuery(PageObject po, String key) {
		return securityDao.pageQuery(po, key);
	}

	public void delete(String secId) {
		securityDao.delete(SmdcSecurity.class, secId);
	}

	public double getBondInterset(SmdcSecurity security, Date date) {
		try {
			if (!SecurityType.isBond(security.getSecType())) {
				return 0;
			}
			if (security.getBondIntrestpayFreq() == null || security.getBondIntrestpayFreq() == 0) {
				return 0;
			}
			ZonedDateTime now = DateUtil.toZoned(date);
			ZonedDateTime lastCuponDate =DateUtil.toZoned(security.getBondMaturity());
			int monthStep = 12 / security.getBondIntrestpayFreq();
			while(lastCuponDate.isAfter(now)){
				lastCuponDate = lastCuponDate.minusMonths(monthStep);
			}
			
			long paydays = Duration.between(lastCuponDate, now).toDays()+1;
			return paydays/365.0 * security.getBondCupon() * security.getBondNotional() / 100;
		} catch (Exception e) {
			return 0;
		}
	}

	public double getBondInterset(String secId, Date date) {
		SmdcSecurity security = get(secId);
		return getBondInterset(security, date);
	}
	
	public boolean isDlist(SmdcSecurity security, Date date) {
		if (security.getDlistDate() != null && date.after(security.getDlistDate())) {
			return true;
		} else {
			return false;
		}
	}
	
	public void deleteWithMarket(String secId){
		securityDao.delete(SmdcSecurity.class, secId);
		fundamentalDao.deleteBySecId(secId);
		marketDao.deleteBySecId(secId);
	}

}
